"""
Case Type   : 连接
Case Name   : 最大密码长度设置和最小密码长度设置大小关系校验
Create At   : 2024/10/15
Owner       : @li-xin12345
Description :
    1、设置密码最大长度为6
    gs_guc set -N all -I all -c "password_max_length = 6"
    2.设置密码最小长度为8
    gs_guc set -N all -I all -c "password_min_length = 8"
    3、清理环境
    gs_guc set -N all -I all -c "password_max_length = 32"
    gs_guc set -N all -I all -c "password_min_length = 8"
Expect      :
    1、成功
    2、失败，提示不能大于最大密码长度
    3、成功
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

pri_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != pri_sh.get_node_num(), '非1+2环境不执行')
class SessionTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.constant = Constant()
        self.common = Common()
        self.params = {'password_max_length': 32, 'password_min_length': 8}
        self.error_msg = 'FATAL:  password_min_length (8) should be no more ' \
                         'than password_max_length (6)'

    def test_connect(self):
        text = '----step1: 设置密码最大长度为6；expect: 设置成功----'
        self.log.info(text)
        param_list = list(self.params.keys())
        res = pri_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, f'{param_list[0]}=6')
        self.log.info(res)
        self.log.info('--步骤2中再重启--')
        self.assertTrue(res)

        text = '----step2: 设置密码最小长度为8；' \
               'expect: 失败，提示不能大于最大密码长度----'
        self.log.info(text)
        res = pri_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, f'{param_list[1]}=8')
        self.log.info(res)
        res = pri_sh.restart_db_cluster(get_detail=True)
        self.log.info(res)
        self.assertIn(self.error_msg, res, f'执行失败：{text}')

    def tearDown(self):
        text = '----step3: 清理环境;expect:成功----'
        self.log.info(text)
        for key, value in self.params.items():
            res = pri_sh.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG, f'{key}={value}')
            self.log.info(res)
        res = pri_sh.restart_db_cluster(get_detail=True)
        self.log.info(res)
        self.assertTrue(res, f'执行失败: {text}')
        text = f'-----{os.path.basename(__file__)} end-----'
        self.log.info(text)